package com.example.lambda;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;

/**
 * @Author: 朝花不迟暮
 * @Description: 使用lambda流求最大，最小值 取流数据元素总数
 * @Date: 2022/11/4
 */
public class MaxMinCountSample {
    public static void main(String[] args) {
        List<UserInfo> userInfoList = new ArrayList<>();
        userInfoList.add(new UserInfo(1L, "捡田螺的小男孩", 18));
        userInfoList.add(new UserInfo(3L, "捡瓶子的小男孩", 26));
        userInfoList.add(new UserInfo(2L, "程序员田螺", 27));

        Optional<UserInfo> maxAgeUserInfoOpt = userInfoList.stream().max(Comparator.comparing(UserInfo::getAge));
        maxAgeUserInfoOpt.ifPresent(userInfo -> System.out.println("max age user:" + userInfo));

        Optional<UserInfo> minAgeUserInfoOpt = userInfoList.stream().min(Comparator.comparing(UserInfo::getAge));
        minAgeUserInfoOpt.ifPresent(userInfo -> System.out.println("min age user:" + userInfo));

        long count = userInfoList.stream().filter(user -> user.getAge() > 18).count();
        System.out.println("大于18岁的用户:" + count);
    }
}
